package com.aaa.web.service.impl.appstoreserviceimpl;

import com.aaa.entity.Employee;
import com.aaa.entity.appstore.ElectromobileMessage;
import com.aaa.util.EmpUtil;
import com.aaa.util.PictureUtil;
import com.aaa.web.commons.Result;
import com.aaa.web.commons.SecondCarEntrepotQuery;
import com.aaa.web.dao.appstoredao.ElectromobilePartsDao;
import com.aaa.web.service.appstoreservice.ElectromobilePartsService;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
配件商城页面
许一鑫
 */
@Service
public class ElectromobilePartsServiceImpl implements ElectromobilePartsService {
    //自动注入dao
    @Autowired
    private ElectromobilePartsDao dao;

    /**
     * 获取所有配件商品信息,分页，搜索框
     * */
    @Override
    public Map getElectromobilePartsMessage(SecondCarEntrepotQuery query) {
        int page=query.getPage();
        int size=query.getLimit();
        PageHelper.startPage(page,size);
        Map map=new HashMap();
        List<ElectromobileMessage> list =dao.getElectromobilePartsMessage(query);
        map.put("data",list);
        map.put("count",dao.getElectromobilePartsCount(query));
        map.put("code",ISystemConst.SU_COD);
        map.put("msg","");
        return map;
    }

    /**根据商品名下架已有的商品*/
    @Override
    public Result delGoods(String goodsname) {
        //设置返回值
        Result result=new Result();
        dao.delGoods(goodsname);
        //提交车主信息成功后返回100
        result.setCode(ISystemConst.SUCCESS_CODE);
        return result;
    }

    //上传照片
    @Override
    public Map uploadPhoto(MultipartFile file) {
        File f = null;
        try {
            f= File.createTempFile("tmp", null);
            file.transferTo(f);
            f.deleteOnExit();
            String imgurl = PictureUtil.getPicture(f);
            Map<String, Object> map = new HashMap<>();
            map.put("data",imgurl);
            map.put("code",ISystemConst.SU_COD);
            map.put("msg","");
            return  map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //添加新商品
    @Override
    public Result addNewGoods(ElectromobileMessage e) {
        //设置返回值
        Result result=new Result();
        //获取员工schoolid
        Employee emp= EmpUtil.getEmp();
        //查重，查询是否该商品名重复
        String goodsname=dao.findgoodsname(e.getGoodsname());
        if (goodsname!=null || goodsname!=""){
            //商品名重复则返回200
            result.setCode(ISystemConst.DEFEAT_CODE);
            return result;
        }
        dao.addNewGoods(e);
        return null;
    }
}
